<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible="visible"
    lock-scroll
    width="800px"
    @close="visible = false"
    @closed="handleResetForm"
  >
    <el-form ref="form" :model="dataForm" :rules="dataFormRules" label-width="120px">
      <el-form-item label="认证类型" prop="category_id">
        <el-select v-model="dataForm.category_id" class="mr-4" placeholder="请选择认证类型">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name" :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="认证标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="请输入认证资料标题(不用写可以敲空格)"/>
      </el-form-item>

      <el-form-item label="认证资料" prop="content">
        <el-input v-model="dataForm.content" placeholder="请输入认证资料" type="textarea"/>
      </el-form-item>

      <el-form-item label="启用状态" prop="is_visible">
        <el-switch
          v-model="dataForm.is_visible"
          active-text="开启"
          inactive-text="关闭"
        >
        </el-switch>
      </el-form-item>
    </el-form>

    <section slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </section>
  </el-dialog>
</template>

<script>
import { presenterSubjectByIdPost, presenterSubjectByIdPut } from '@/api/KTV'

const type = {
  add: '新增',
  edit: '编辑'
}

const rules = {
  category_id: [
    { required: true, trigger: 'blur' }
  ],
  title: [
    { required: true, message: '请输入认证内容标题', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入认证内容', trigger: 'blur' }
  ],
  is_visible: [
    { required: true, trigger: 'blur' }
  ]
}

export default {
  name: 'TCForm',
  props: {
    typeList: {
      type: Array,
      require: true
    },
    initData: {
      type: Function,
      require: true
    }
  },
  data() {
    return {
      visible: false,
      title: type.add,
      dataFormRules: rules,
      dataForm: {
        id: undefined,
        category_id: undefined,
        title: '',
        content: '',
        is_visible: true
      }
    }
  },
  computed: {
    form() {
      return this.$refs.form
    }
  },
  methods: {
    show(entity) {
      if (entity) {
        this.title = type.edit
        this.dataForm.id = entity.id
        this.dataForm.category_id = entity.category_id
        this.dataForm.title = entity.title
        this.dataForm.content = entity.content
        this.dataForm.is_visible = !!entity.is_visible
      } else {
        this.title = type.add
      }
      this.visible = true
    },

    // 取消操作
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },

    handleSubmit() {
      this.form.validate(async(valid, values) => {

        if (!values) return

        let tempFormData = JSON.parse(JSON.stringify(this.dataForm))
        tempFormData.is_visible = ~~tempFormData.is_visible

        if (this.title === type.add) {

          try {
            await presenterSubjectByIdPost(tempFormData)
            this.$message.success('创建成功')
            this.initData()
          } catch (e) {
          }

        } else if (this.title === type.edit) {
          try {
            await presenterSubjectByIdPut(tempFormData)
            this.$message.success('编辑成功')
            this.initData()
          } catch (e) {

          }
        }

        this.visible = false
        this.form.resetFields()
      })
    },

    handleResetForm() {
      this.dataForm = {
        id: undefined,
        category_id: undefined,
        title: '',
        content: '',
        is_visible: true
      }
    }
  }
}
</script>

<style scoped>

</style>
